package Test07;

//多线程

import java.util.concurrent.ArrayBlockingQueue;

/*
需求:利用阻塞队列完成生产者和消费者(等待唤醒机制)的代码
细节:
生产者和消费者必须使用同一个阻塞队列
 */

public class Test14 {
    public static void main(String[] args) {

        //创建阻塞队列的对象
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(1);

        //创建线程对象
        Cook14 c = new Cook14(queue);
        Foodid14 f = new Foodid14(queue);

        //开启线程
        c.start();
        f.start();
    }
}

class Cook14 extends Thread {

    ArrayBlockingQueue<String> queue;

    public Cook14(ArrayBlockingQueue<String> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        while (true) {
            try {
                queue.put("面条");
                System.out.println("厨师放了一碗面条");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Foodid14 extends Thread {

    ArrayBlockingQueue<String> queue;

    public Foodid14(ArrayBlockingQueue<String> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        while (true) {
            try {
                String food = queue.take();
                System.out.println(food);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
